﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;

namespace Runda.EShop.SaaS.CustomerManage
{
    public partial class EnterpriseEdit : BasePage
    {
        public Guid EntID
        {
            get
            {
                return new Guid(Request.QueryString["EntID"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            txtBusinessLicenseDeadline.Attributes.Add("onclick", "WdatePicker();");
            txtDrugLicenseDeadline.Attributes.Add("onclick", "WdatePicker();"); 
            txtApparatusLicenseDeadline.Attributes.Add("onclick", "WdatePicker();");

            if (!IsPostBack)
            {
                BizEnterpriseEntity entity = new BizEnterpriseBll().Get<BizEnterpriseEntity>(EntID);
                txtEntCode.Text = entity.EntCode;
                txtEntName.Text = entity.EntName;
                if (entity.BusinessLicenseDeadline != DateTime.MinValue)
                {
                    txtBusinessLicenseDeadline.Text = entity.BusinessLicenseDeadline.ToString("yyyy-MM-dd");
                }
                if (entity.ApparatusLicenseDeadline != DateTime.MinValue)
                {
                    txtApparatusLicenseDeadline.Text = entity.ApparatusLicenseDeadline.ToString("yyyy-MM-dd");
                }
                if (entity.DrugLicenseDeadline != DateTime.MinValue)
                {
                    txtDrugLicenseDeadline.Text = entity.DrugLicenseDeadline.ToString("yyyy-MM-dd");
                }
                ddlExpired.SelectedValue = entity.Expired ? "1" : "0";
                txtBussinessScope.Text = entity.BusinessScope;
                rbcCo.SelectedValue = entity.cCO;
                rbEntType.SelectedValue = entity.EntType.ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            BizEnterpriseBll bll = new BizEnterpriseBll();
            BizEnterpriseEntity entity = bll.Get<BizEnterpriseEntity>(EntID);

            entity.EntCode = txtEntCode.Text;
            entity.EntName = txtEntName.Text;

            DateTime dt =DateTime.Now;

            if (DateTime.TryParse(txtBusinessLicenseDeadline.Text, out dt))
            {
                entity.BusinessLicenseDeadline = dt;
            }

            if (DateTime.TryParse(txtApparatusLicenseDeadline.Text, out dt))
            {
                entity.ApparatusLicenseDeadline = dt;
            }

            if (DateTime.TryParse(txtDrugLicenseDeadline.Text, out dt))
            {
                entity.DrugLicenseDeadline = dt;
            }
            entity.Expired = ddlExpired.SelectedValue == "1" ? true : false;
            entity.BusinessScope = txtBussinessScope.Text;
            entity.cCO = rbcCo.SelectedValue;
            entity.EntType = Convert.ToInt16(rbEntType.SelectedValue);

            bll.Update(entity);

            ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('保存成功.');parent.$.ligerDialog.hide();", true);
        }
    }
}